var download_manager = liwe.module ( 'download_manager' );

// ===================================================================
// PUBLIC ATTRIBUTES
// ===================================================================

// path to media folder
download_manager.file_base_path = '/site/download_manager/';
// destination div for DM
download_manager.target_div = 'block_main';
// default module
download_manager._module = 'download_manager';
// filter form id
download_manager.filter_form_id = 'filter_form';
download_manager.upload_form_id = 'upload_form';
download_manager._resize_cache = {};
// dataset templates templates
download_manager._def_templates = {	
					'details': {    'row' :'res-table-cell-details',
							'table_start' : 'res-table-open',
							'table_header' : 'res-table-cell-details-head',
							'table_close' : 'res-table-close'
						},
					'full-details': { 'row' : 'res-table-cell-full-details',
							'table_start' : 'res-table-open',
							'table_header' : 'res-table-cell-full-details-head',
							'table_close' : 'res-table-close'
						},
					'icons' : { 'row' : 'res-table-cell-icons',
						    'table_start' : 'res-div-open',
						    'table_header' : '',
						    'table_close' : 'res-div-close'
						}
					    
				};
download_manager.curr_templates = download_manager._def_templates;

// ===================================================================
// PRIVATE ATTRIBUTES
// ===================================================================
// init vars
// default browse option values 
download_manager._page_offset = 10;
download_manager._page_view = 'icons';
download_manager._order_by = 'name';
// default views
download_manager._sidebar_div = 'DM-sidebar';
download_manager._topbar_div  = 'DM-topbar-pagination';
download_manager._browser_div = 'DM-browser';
download_manager._results_div = 'DM-results';
//download_manager._topbar_div  = 'DM-topbar';
// temp internal vars
download_manager._temp_details = { empty : "empty" };
download_manager._ext_list = [];
download_manager._mods_list = [];
download_manager._curr_tags = '';
download_manager._browse_files_params = {};
	
download_manager.init = function ()
{
	download_manager._get_ext_list ();
	download_manager._get_mods_list ();

	download_manager.load_templates ( download_manager.init_mod );
	download_manager.cbacks = {
					'image_click' : null
				};
};

download_manager.set_module = function ( module, form_name )
{
	if ( ! module && form_name )
	{
		var f = liwe.form.get ( form_name );
		if ( ! f ) return;
		var vals = f.get_values ();
		module = vals.module;
	}
	download_manager._module = module;
};

download_manager.set_results_div = function ( res_div )
{
	download_manager._results_div = res_div;
};

/* download_manager.set_results_templates
 * param: tmpl_dict ( object ) 		dictionary with tamplates names based on type of lay-out
 * param: page_view ( string ) 		type of data set lay-out ( list, details, icons )
 * set custom templates	
 */
download_manager.set_results_templates = function ( tmpl_dict, page_view )
{
	if ( tmpl_dict )
		download_manager.curr_templates.inherits ( tmpl_dict, true );
	if ( page_view )
		download_manager._page_view = page_view;
				
	//console.debug ( "DOWNLOAD_MANAGER - set_results_templates: curr_templates %o", download_manager.curr_templates);
	download_manager._get_ds_templates ();
};

download_manager.reset_templates = function ( page_view )
{
	download_manager.curr_templates = download_manager._def_templates;
	download_manager._get_ds_templates ( '', page_view );
};

/* download_manager._get_ds_templates
 * param: tmpl_dict ( object ) 		dictionary with tamplates names based on type of lay-out
 * param: page_view ( string ) 		type of data set lay-out ( list, details, icons )
 *
 * get layout on _page_view value. Params are optionals and they shouldn't be used.	
 */
download_manager._get_ds_templates = function ( tmpl_dict, page_view )
{
	if ( ! download_manager.ds ) download_manager.ds = new DataSet ( 'results' );
	if ( ! tmpl_dict ) tmpl_dict = download_manager.curr_templates;
	if ( ! page_view ) page_view = download_manager._page_view;
	if ( ! download_manager.templates ) 
	{
		setTimeout ( function () { download_manager._get_ds_templates ( tmpl_dict, page_view ); }, 200 );
		return;
	}
	var table_start, table_header, table_close, table_row;
	var DM_tmpl = download_manager.templates;

	function _build_single_row ( start, middle, end )
	{
		var render = new String.buffer ();

		render.add ( DM_tmpl [ start ]);
		render.add ( DM_tmpl [ middle ] );
		render.add ( DM_tmpl [ end  ] );

		return render.toString ();
	};

	// paginator templates
	download_manager.ds.paginator.templates [ 'pag-first' ] = download_manager.templates [ 'pag-first' ];
	download_manager.ds.paginator.templates [ 'pag-last' ]  = download_manager.templates [ 'pag-last' ];
	download_manager.ds.paginator.templates [ 'pag-prev' ]  = download_manager.templates [ 'pag-prev' ];
	download_manager.ds.paginator.templates [ 'pag-next' ]  = download_manager.templates [ 'pag-next' ];
	download_manager.ds.paginator.templates [ 'pag-pos' ]   = '%(_PAGE)s';
	download_manager.ds.paginator.templates [ 'pag-sep' ]   = '&nbsp;&nbsp;';

	switch ( page_view )
	{
		// results body templates
		case 'list':
			table_start = DM_tmpl [  tmpl_dict [ 'details' ][ 'table_start' ] ]; 
			table_header = DM_tmpl [  tmpl_dict [ 'details' ][ 'table_header' ] ]; 
			table_close = DM_tmpl [  tmpl_dict [ 'details' ][ 'table_close' ] ]; 
			table_row = _build_single_row ( 'res-single-row-open', tmpl_dict [ 'details' ][ 'row' ] , 'res-single-row-close' );

		break;
		case 'details':
			table_start = DM_tmpl [   tmpl_dict [ 'full-details' ][ 'table_start' ] ]; 
			table_header  = DM_tmpl [   tmpl_dict [ 'full-details' ][ 'table_header' ] ]; 
			table_close  = DM_tmpl [   tmpl_dict [ 'full-details' ][ 'table_close' ] ]; 
			table_row = _build_single_row ( 'res-single-row-open', tmpl_dict [ 'full-details' ][ 'row' ] , 'res-single-row-close' );
		break;
		default:
			table_start = DM_tmpl [  tmpl_dict [ 'icons' ][ 'table_start' ] ];
			table_header = DM_tmpl [   tmpl_dict [ 'icons' ][ 'table_header' ] ]; 
			table_close = DM_tmpl [  tmpl_dict [ 'icons' ][ 'table_close' ] ];
			table_row = _build_single_row ( 'res-table-row-open', tmpl_dict [ 'icons' ][ 'row' ], 'res-table-row-close' );
		break;
	}

	download_manager.ds.templates [ 'table_start' ]  = table_start;
	download_manager.ds.templates [ 'table_header' ] = table_header; 
	download_manager.ds.templates [ 'table_end' ]    = table_close;
        download_manager.ds.templates [ 'table_row' ]    = table_row;
	//console.debug ( " --- download_manager._get_ds_templates: table_row = %s " , table_row );
        //download_manager.ds.templates [ 'table_row' ] = '%(_render)s';
};

download_manager._get_ext_list = function ( cback )
{
	download_manager.ajax ( { "action" : "download_manager.ajax._get_ext_list" }, function ( data )
                                        {
						download_manager._ext_list.push ( { label : "" , value : "" } );
                                                data [ 'ext_list' ].iterate ( function ( v, k )
										{
											 download_manager._ext_list.push ( { label : v [ 'fext' ] , value : v [ 'fext' ] } );
										}
									     );
						if ( cback ) cback ();
                                        });
	
};

download_manager._get_mods_list = function ( cback )
{
	download_manager.ajax ( { "action" : "download_manager.ajax._get_mods_list" }, function ( data )
                                        {
						download_manager._mods_list.push ( { label : "download_manager" , value : "download_manager" } );
                                                data [ 'mods_list' ].iterate ( function ( v, k )
										{
											 download_manager._mods_list.push ( { label : v [ 'module' ] , value : v [ 'module' ] } );
										}
									     );
						if ( cback ) cback ();
                                        });
	
};

download_manager.image_click = function ( path_name, file_name )
{
	if (! download_manager.cbacks [ 'image_click' ] ) 
	{
		download_manager.download_file ( path_name, file_name );
		return;
	}
	download_manager.cbacks [ 'image_click' ] ( path_name, file_name );
};
download_manager.download_file = function ( path_name , file_name )
{
	var furl = String.format ( 'ajax.pyhp?action=download_manager.ajax.download_file&path_name=%s&file_name=%s', path_name, file_name );
	window.open ( furl );	
};
download_manager.init_mod = function ()
{
	download_manager._get_ds_templates ();
};

download_manager._init_tabs = function ( dest_div )
{
	var s = new String.buffer ();
	var res;

	if ( ! download_manager.tabs ) download_manager.tabs = new OS3Tabs.instance ( 'DM-tabs' );

	download_manager.tabs.clear ();

	download_manager.tabs.set_group ( 'qe_news' );

	s.add ( download_manager.templates [ 'tabs_start' ] );

	s.add ( String.formatDict ( download_manager.templates [ 'tabs_tab' ], { "tab_name" : "DM-filter-form" } ) );
	download_manager.tabs.add ( 'Filter', 'DM-filter-form' );

	if ( download_manager.get ( 'admin' ) )
	{
		s.add ( String.formatDict ( download_manager.templates [ 'tabs_tab' ], { "tab_name" : "DM-upload-form" } ) );
		download_manager.tabs.add ( 'Upload', 'DM-upload-form' );
	}

	s.add ( download_manager.templates [ 'tabs_end' ] ); 

	res = s.toString ();

	$( dest_div, res );

	download_manager.tabs.render ();

	if ( download_manager.get ( 'admin' ) ) download_manager.tabs.enable_tab ( 'DM-upload-form', 'enabled' );
	download_manager.tabs.enable_tab ( 'DM-filter-form', 'enabled' );
	download_manager.tabs.send_event ( 'DM-filter-form', 'click' );
};

/* download_manager.render ();
	param: dest_div ( string )	destination div for DM ( mandatory )
	param: module ( string )	module name
	return: nothing		
*/
download_manager.render = function ( dest_div, module )
{
	// Set module. If any module name is passed has param then it will set default
	var curr_module = ( module ) ? module : 'download_manager'; //download_manager._module;
	download_manager.set_module ( curr_module );
	if ( ! dest_div ) dest_div = download_manager.dest_div;
	// create interface throught templates
	$( dest_div , download_manager.templates [ "interface" ] ); 
	download_manager._init_tabs ( 'DM-filter-form-cnt' );
	// assign interface contents
	download_manager.browse_option ( download_manager._topbar_div );
	// sidebar elements
	download_manager.filter_form ( 'DM-filter-form' );
	if ( download_manager.get ( 'admin' ) ) download_manager.admin.upload_form ();
};
/* download_manager.set_pagination ();
	param: val (int)	number of results per page
	return: nothing
	set pagination var and call browse_files function ( do we need it ?? )
*/
download_manager.set_pagination = function ( val )
{
	download_manager._page_offset = val;
	download_manager._get_ds_templates ();
	download_manager.browse_files ( download_manager._results_div );
};

/* download_manager.set_page_view ();
	param: val ( string ) 	type of view  (values: icons,details )
	return: nothing
*/
download_manager.set_page_view = function ( val )
{
	download_manager._page_view = val;
	download_manager._get_ds_templates ();
	download_manager.browse_files ( download_manager._results_div );
};
/* download_manager.set_order_by ();
	param: val ( string ) 	order by  (values: name,fext,created )
	return: nothing
	set order by preference 
*/
download_manager.set_order_by = function ( val )
{
	download_manager._order_by = val;
	download_manager._get_ds_templates ();
	download_manager.browse_files ( download_manager._results_div );
};
/* download_manager.filter_form ();
	param: dest_div ( string )	destination div ( mandatory )
	return: nothing
	Create filter form for searching otpions
*/
download_manager.filter_form = function ( dest_div ) 
{

	var f = new liwe.form.instance ( download_manager.filter_form_id );
	// create filter form
        //f.hidden ( "module", download_manager._module );
	f.select ( { name: "module", options: download_manager._mods_list, force_select: false, style: "width:150px;", value: download_manager._module, onchange: "download_manager.set_module ( this.value );" } );
        f.hidden ( "id_obj", "" );
	f.label ( { value: "Nome file" } );
        f.text   ( { name: "name", maxlength: 200 } );
	f.label ( { value: "Descrizione" } );
        f.text   ( { name: "descr", maxlength: 200 } );
	f.label ( { value: "Estensione" } );
	f.select ( { name: "fext", options: download_manager._ext_list, force_select: false, style: "width:150px;" } );
	f.label ( { value: "Tags" } );
        f.tags   ( { name: "filters_tags", module: 'download_manager', size: '20' } ); //download_manager._module
        f.button ( { value: "Cerca", 'class': 'green', onclick: "download_manager.browse_files( '" + download_manager._results_div + "' );" } );
        f.button ( { value: "Reset", 'class': 'yellow', onclick: "download_manager.reset_form( '" + download_manager.filter_form_id + "' );" } );

        f.set ( dest_div );

};
/* download_manager.browse_option ();
	param: dest_div ( string )	destination div ( mandatory )
	return: nothing
	Create filter form for browsing options ( pagination, sorting etc )
*/
download_manager.browse_option = function ( dest_div )
{

	var f = new liwe.form.instance ( 'browse_option' );

	var pagination_opts = [
				{ label: "10", value: "10" },
				{ label: "20", value: "20" },
				{ label: "30", value: "30" },
				{ label: "*", value: ">" }
			      ];
	f.select ( { label: "per page", name: "pagination", options: pagination_opts, force_select: false, nonl: true, onchange: 'download_manager.set_pagination ( this.value );' } );

	var view_opts = [
			 { label: "icone", value: "icons" },
			 { label: "lista", value: "list" },
			 { label: "dettagli", value: "details" }
		        ];

	f.select ( { label: "page view", name: "page_view", options: view_opts, force_select: false, nonl: true, onchange: 'download_manager.set_page_view ( this.value );' } );
	
	var order_opts = [
			 { label: "nome", value: "name" },
			 { label: "estensione", value: "fext" },
			 { label: "creato", value: "created" }
		        ];

	f.select ( { label: "ordinamento", name: "order_by", options: order_opts, force_select: false, nonl: true, onchange: 'download_manager.set_order_by ( this.value );' } );
	//f.button ( { label: "upload", value: "upload", onclick : "download_manager.admin.upload_form ( 'DM-upload-form' );" } );

	f.set ( dest_div );
};
/* download_manager.reset_form ();
	param: form_id ( string )	element id of the form to be cleared
	return: nothing
	generic function to clear form values
*/
download_manager.reset_form = function ( form_id ) 
{
	var frm = liwe.form.get ( form_id );
	frm.clear ();
};
/* download_manager._dl_link ()
	param: file_path ( string ) 	file path
	get file's mime type 
*/
download_manager._dl_link = function ( file_path )
{
	download_manager.ajax ( { "action" : "download_manager.ajax._define_mime_type", "path_name" : file_path }, function ( data )
					{
						//console.debug ( data );
					});
};
/* download_manager.browse_files ();
	param: dest_div ( string )	destination div
	param: tags ( string )		selected tags separated by pipes
	param: id_obj ( int )		relation id
	param: module ( string )	select module 
	param: params ( dict )		params for search
	return: nothing
	query database with searching criteria and render results
*/
download_manager.browse_files = function ( dest_div, tags, id_obj, module, params )
{	
	if ( ! dest_div ) dest_div = download_manager._results_div; 
	if ( ! module ) module = download_manager._module;
	var vals = {};
	// get all values from filter form and use it as condition
	if ( $( download_manager.filter_form_id ) )
	{
		var frm = liwe.form.get ( download_manager.filter_form_id );
		vals = frm.get_values();
	} else if ( params )
	{
		vals = params;
	}

	var tpl_type = vals.get ( "template", "admin" );

	// set current selected tags 
	//download_manager._curr_tags = 'immagini|prova';
	download_manager._curr_tags =  vals.get ( 'filters_tags', '' );
	if ( tags ) 
	{
		download_manager._curr_tags =  tags ; 
		vals [ 'filters_tags' ] =  tags ; 
	}

	if ( download_manager._browse_files_params.id_obj ) vals [ 'id_obj' ] = download_manager._browse_files_params.id_obj;
	if ( download_manager._browse_files_params.module ) vals [ 'module' ] = download_manager._browse_files_params.module;
	if ( id_obj ) vals [ 'id_obj' ] = id_obj;
	if ( module ) vals [ 'module' ] = module;

	download_manager._tags_tree_res ( download_manager._curr_tags );
	// search opions
	if ( $( "browse_option" ) )
	{
		var frm_opts = liwe.form.get ( "browse_option" );
		var opts = frm_opts.get_values();
		vals [ 'page_view' ] = opts [ 'page_view' ];
		vals [ 'order_by' ]  = opts [ 'order_by' ];
	}	
	// pagination 
	download_manager.ds.lines_per_page =  vals.get ( 'lines_per_page', download_manager._page_offset );
	if ( vals.get ( 'page_view' ) ) download_manager._page_view = vals [ 'page_view' ];
	if ( vals.get ( 'order_by' ) ) download_manager._order_by = vals [ 'order_by' ];
	// store params
	download_manager._browse_files_params = vals;
	// call ajax.py function for db query
	if ( ! vals.get ( 'action' ) ) vals [ 'action' ] = 'download_manager.ajax.browse_files';
	download_manager.ds.set_fields ( vals );
	download_manager.ds.cbacks [ 'row_manip' ] = download_manager._row_manip;
	//if ( vals.get ( 'fill_end_cback' ) ) download_manager.ds.cbacks [ 'fill_end' ] = vals [ 'fill_end_cback' ];
	var ds_cback =  vals.get ( 'ds_cback' , {} );
	ds_cback.iterate ( function ( v, k ) 
				{
					//console.debug ( "download_manager - ds_cbacks = %s / %s" , k , v );
					download_manager.ds.cbacks [ k ] = eval ( v ); 
				} );
	download_manager.ds.fill ( function () { download_manager.ds.render ( dest_div ); } ); 

};

/* download_manager._row_manip ();
	param: ds ( object )		dataset
	param: row ( object ) 		current row
*/
download_manager._row_manip = function ( ds, row )
{
	/* row response
	"name": "Firefox_wallpaper.png", "descr": "", "fext": "png", "created": "2010-08-31 12:03:43", 
	"_pos": 0, "id": 1
	*/
	var main_dir = String.format ( "%3.3d" , row [ 'id' ] , 1000 );
	var sub_dir = String.format ( "%2.2d" , row [ 'id' ] , 100 );
	var file_path = String.format ( "%s%s/%s/", download_manager.file_base_path, main_dir, sub_dir );
	row [ "thumb_path" ] = file_path ;
	row [ "thumb_name" ] =  row [ 'id' ];
	row [ "thumb_ext" ]  =  'jpg';

	var file_path = row [ "thumb_path" ] + row [ "name" ];
	var file_id_path = String.format ( "%s%s%s%s" , row [ "thumb_path" ] , row [ "id" ] , "." , row [ "fext" ] );
	row [ 'dl_link' ] = row.get ( 'mime', '' ); //download_manager._dl_link ( file_path );
	row [ 'link_no_slash' ] = file_id_path.substr ( 1 );
	row [ 'file_options' ]  = String.formatDict ( download_manager.templates [ 'file_options' ], row );
	row [ 'file_options_user' ]  = String.formatDict ( download_manager.templates [ 'file_options_user' ], row );
	// convert files size
	row [ '_Size' ] = download_manager._byte_conv ( row.get ( 'Size', 0 ) );
	// convert date fields
	row [ '_LastModified' ] = download_manager._date_conv ( row.get ( 'LastModified', '0000-00-00' ) );
	row [ '_CreationTime' ] = download_manager._date_conv ( row.get ( 'CreationTime', '0000-00-00' ) );
	row [ '_LastAccessed' ] = download_manager._date_conv ( row.get ( 'LastAccessed', '0000-00-00' ) );
	//row [ '_render' ] = download_manager._build_row ( dct );
};
/* download_manager.file_details ()
	param: file_path ( string )	complete file path
	param: id ( string )		file id
	return: nothing
	it sets gloabl var _temp_details with file details
*/
download_manager.file_details = function ( file_path, id )
{
	if ( file_path )
	{
		
		download_manager.ajax ( { "action" : "download_manager.ajax.file_details", "path_name" : file_path, "id_file": id } , function ( data )
						{
							if ( data [ 'details' ] )
							{
								//console.debug ( data [ 'details' ] );
								data [ 'details' ][ 'Size' ] = download_manager._byte_conv ( data [ 'details' ][ 'Size' ] );
								data [ 'details' ][ 'file_name' ] = String.basename ( file_path ); 
								//$( 'DM-details-results' , String.formatDict ( download_manager.templates [ 'details-box' ], data [ 'details' ] ) );
								liwe.utils.notifier.show ( String.formatDict ( download_manager.templates [ 'details-box' ], data [ 'details' ] ), 'info' );
								download_manager._temp_details = data [ 'details' ];
							}
						} );
		//return download_manager._temp_details;
	}
};

/* download_manager.dl_box ()
	param: params ( object )	filter params 
	param: dest_div ( string ) 	render div
	param: module ( string ) 	module name 
	param: view ( string ) 		list/icon/detail
	param: tmpl ( string ) 		template to use
	
	return html of search result to destination div
	NOTE: you can pass dataset cbacks trough params { 'ds_cback' : { 'fill_end' : 'my.function' } }
*/
download_manager.dl_box = function ( params, dest_div, module, view, tmpl )
{
        if ( ! dest_div || ! params ) return;
	if ( ! module ) module = params.get ( 'module', 'download_manager' ); 
        if ( ! tmpl ) 
	{
		tmpl = params.get ( 'tmpl', '' );
		if ( tmpl == '' )
		{
			//FIXME: non funzionano i templates personalizzati
			/*
			tmpl = {}; // download_manager._def_templates.clone ();
			tmpl = {
					'icons' : { 'row' : download_manager.templates [ 'user-table-cell-icons' ] },
					'details' : { 'row' : download_manager.templates [ 'user-table-cell-details'  ] },
					'full-details' : { 'row' : download_manager.templates [ 'user-table-cell-full-details' ] } 
				};
			*/
			tmpl = download_manager.curr_templates;
		}
	}
        if ( ! view ) view = params.get ( 'view', 'icons' );

        download_manager.set_results_templates ( tmpl, view );

        download_manager.browse_files ( dest_div, '', '', module, params );
};

download_manager.plugin = function ( el, params )
{
	download_manager.dl_box ( params, el ); // , module, view, tmpl )
};

/* download_manager._byte_conv ()
	param: bytes ( decimal )	file size in bytes
	return: result ( string ) 	string with converted file size
*/
download_manager._byte_conv = function ( bytes )
{
	var conv = "Bytes"; 
	var coef = 1;
	if ( bytes > 1024 ) 
		{ 
		  conv = "Kb";
		  coef =  1024; 
		}
	if ( bytes > 1048576 ) 
		{ 
		  conv = "Mb" 
		  coef =  1048576; 
		}
	return  result = String.format ( "%d %s" , Math.round ( bytes / coef ) , conv );
};
/* download_manager._date_conv ()
	param: date_str ( string )	date in string format
	return: frm_d ( string ) 	formatted date in string format
*/
download_manager._date_conv = function ( date_str )
{
	var d = new Date ( date_str );
	// FIXME : date2str non converte correttamente questo formato di data
	var temp_d = String.format ( "%s-%s-%s" , d.getFullYear (), (d.getMonth () + 1 ), d.getDate () );//liwe.utils.date2str ( d, 1 );
	var temp_h = d.getHours();
	temp_h = ( temp_h < 10 ) ? '0' + temp_h : temp_h ;
	var temp_m = d.getMinutes();
	temp_m = ( temp_m < 10 ) ? '0' + temp_m : temp_m ;

	frm_d = String.format ( "%s %s:%s" , temp_d, temp_h, temp_m );
	return frm_d;
};

/* download_manager._expand_tag_tree ();
	param: node 		tree node
	recursive function which creates tree nodes
*/
download_manager._expand_tag_tree = function ( node )
{
	var vals = {
			action : "tags.ajax.get_relative_tags",
			module : "download_manager", //download_manager._module,
			id_site: 1,
			tags   : node.val
		   };
	count = 0;
	download_manager.ajax ( vals , function ( v ) 
					{ 

						var tags = v [ "tags" ];
						tags.iterate ( function ( t )
						{
							t.id = count++;
							t.val = node.val + "|" + t.name;
							//console.debug ( node.val + ' - ' + t.name );
							t.onclick = "download_manager._get_node_val";
							t.text = String.formatDict ( download_manager.templates [ 'DM-tree-folder' ], t );
							t.folder = true;
						} );

						node.expand_done ( tags );
					} );
};

/* download_manager._get_node_val ();
	param: tree_name 	tree instance name
	param: node_name 	node name
	param: id_obj 		id object
	param: module 		module name 
	pass node values to browse_files function
*/
download_manager._get_node_val = function ( tree_name, node_name, id_obj, module )
{
	var tree = OS3Tree.instances [ tree_name ];
	var node = tree.nodes [ node_name ];

	download_manager.browse_files ( '', node.vali, id_obj, module );
};

/* download_manager._tags_tree_res ();
	param: root		starting tags separated by pipe
	create tree nodes first level 
*/
download_manager._tags_tree_res = function ( root )
{
	if ( !root ) return;

	var tree = new OS3Tree.instance ( "tags_tree" );
	tree.scrollbars = true;

	tree.expand_request = download_manager._expand_tag_tree;
	var sel_tags = root.split ( '|');
	var tags = sel_tags; 
	var dct = {};
	tags.iterate ( function ( t, i ) 
	{
		var txt = String ( t );
		dct = { name : txt }; 
		tree.add_folder ( null, String.formatDict ( download_manager.templates [ 'DM-tree-folder' ], dct ) , "node_" + i , txt, '' ); 
	} );

	tree.render ( "DM-tags-results" );
	/*
	download_manager.ajax ( { action: "tags.ajax.get_relative_tags", id_site: 1, module: 'download_manager', tags: root } , function ( data )
						{
							var tree = new OS3Tree.instance ( "tags_tree" );
							tree.scrollbars = true;

							tree.expand_request = download_manager._expand_tag_tree;
							var tags = data [ "tags" ];
							tags.iterate ( function ( t, i ) 
							{
								var txt = String ( t [ "name" ] );
								tree.add_folder ( null, 
									'<img alt="" border="0" class="tree_folder" src="/gfx/folder.gif" />' + txt, 
									"node_" + i , 
									txt, 
									'' ); 
							} );

							tree.render ( "DM-tags-results" );
						} );
	*/
};

download_manager._animating = function ( target_div, el_butt, visible )
{
	if ( visible ) 
	{
		var style = $( target_div ).style;
		style.overflow = 'visible';
		style.width = '';
		style.height = '';
	}
	if ( el_butt ) $( el_butt ).style.display = 'block';
	download_manager._resize_cache [ target_div ][ 'animate' ] = 0;
};
download_manager._show_div = function ( target_div, force )
{
	if ( ! $( target_div ) ) return;
	if ( force ) download_manager._resize_cache = {};

	var cache = download_manager._resize_cache;

	var size = liwe.dom.get_size ( $( target_div ) );
	var w = size [ 0 ]; 
	var h = size [ 1 ]; 
	var dest_h, visible = false;
	var el_butt = target_div + '-div-butt' ;


	$( target_div ).style.overflow = 'hidden';

	if ( h > 1 ) 
	{
		if ( ! cache.get ( target_div ) || h != cache [ target_div ][ 'h' ] )
		{
			download_manager._resize_cache [ target_div ] = { 'w' : w, 'h' : h, 'animate' : 1 };
			$( target_div ).style.height = h + 'px';
		}
		dest_h = 0;
	} else {
		dest_h = download_manager._resize_cache [ target_div ][ 'h' ];
		download_manager._resize_cache [ target_div ][ 'animate' ] = 1;
		visible = true;
	}

        if ( $( el_butt ) )
        {
                var butt_class = ( dest_h > 0 ) ? 'close' : 'open' ;
                $( el_butt ).className = "DM-butt-" + butt_class;
		$( el_butt ).style.display = 'none';
		//console.debug ( "--_ show_div: className :  %s" , $( el_butt ).className );
        }

	//console.debug ( "download_manager - _show_div: w = %s , h = %s, dest_h = %s", w, h, dest_h );
	liwe.fx.resize ( target_div, w + 'px', dest_h + 'px', function () { download_manager._animating ( target_div, el_butt, visible ); } );

};

